package com.arvin.faceDetect.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.arvin.faceDetect.R
import com.arvin.faceDetect.databinding.ItemFaceBinding
import com.arvin.faceDetect.db.FaceEntity
import com.google.android.material.card.MaterialCardView

class FaceListAdapter(
    private val onItemClick: (FaceEntity) -> Unit,
    private val onItemLongClick: (FaceEntity) -> Boolean
) : ListAdapter<FaceEntity, FaceListAdapter.ViewHolder>(FaceDiffCallback()) {

    private val selectedItems = mutableSetOf<String>()
    var isSelectionMode = false
        private set

    fun isSelected(face: FaceEntity): Boolean {
        return selectedItems.contains(face.id)
    }

    fun toggleSelection(face: FaceEntity) {
        if (selectedItems.contains(face.id)) {
            selectedItems.remove(face.id)
        } else {
            selectedItems.add(face.id)
        }
        notifyItemChanged(currentList.indexOf(face))
    }

    fun clearSelection() {
        selectedItems.clear()
        notifyDataSetChanged()
    }

    fun getSelectedItems(): List<FaceEntity> {
        return currentList.filter { isSelected(it) }
    }

    fun selectAll() {
        selectedItems.addAll(currentList.map { it.id })
        notifyDataSetChanged()
    }

    inner class ViewHolder(
        private val binding: ItemFaceBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(face: FaceEntity) {
            binding.apply {
                face.faceImage?.let { bitmap ->
                    ivFace.setImageBitmap(bitmap)
                }

                ivFace.post {
                    val width = ivFace.width
                    val height = (width * 1.25f).toInt()
                    val params = ivFace.layoutParams
                    params.height = height
                    ivFace.layoutParams = params
                }
                
                tvName.text = face.name
                root.setOnClickListener { onItemClick(face) }
                root.setOnLongClickListener { onItemLongClick(face) }

                val isSelected = isSelected(face)
                checkBox.visibility = if (isSelectionMode) View.VISIBLE else View.GONE
                checkBox.isChecked = isSelected

                (root as MaterialCardView).apply {
                    isChecked = isSelected
                    strokeWidth = if (isSelected) 2.dpToPx(context) else 1.dpToPx(context)
                    strokeColor = if (isSelected)
                        context.getColor(R.color.purple_500)
                    else context.getColor(R.color.outline)
                }

                if (isSelected) {
                    checkBox.animate()
                        .scaleX(1.2f)
                        .scaleY(1.2f)
                        .setDuration(150)
                        .withEndAction {
                            checkBox.animate()
                                .scaleX(1f)
                                .scaleY(1f)
                                .setDuration(100)
                                .start()
                        }
                        .start()
                }
            }
        }
    }

    private fun Int.dpToPx(context: Context): Int {
        return (this * context.resources.displayMetrics.density).toInt()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemFaceBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
}

private class FaceDiffCallback : DiffUtil.ItemCallback<FaceEntity>() {
    override fun areItemsTheSame(oldItem: FaceEntity, newItem: FaceEntity): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: FaceEntity, newItem: FaceEntity): Boolean {
        return oldItem == newItem && oldItem.name == newItem.name
    }
} 